/********************************************************************************/
/*										*/
/* Screen rotate utility							*/
/* v 0.2 date 24/1/2010								*/
/*										*/
/* Copyright 2010 Lev Meirovitch						*/
/* <LVMTime@gmail.com>								*/
/*										*/
/*										*/
/* This program is free software: you can redistribute it and/or modify		*/
/* it under the terms of the GNU General Public License as published by		*/
/* the Free Software Foundation, either version 3 of the License, or		*/
/* (at your option) any later version.						*/
/*										*/
/* This program is distributed in the hope that it will be useful,		*/
/* but WITHOUT ANY WARRANTY; without even the implied warranty of		*/
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		*/
/* GNU General Public License for more details.					*/
/*										*/
/* You should have received a copy of the GNU General Public License		*/
/* along with this program. If not, see <http://www.gnu.org/licenses/>. 	*/
/*										*/
/********************************************************************************/

#include <Xlib.h>
#include <Elementary.h>


/* main window dimentions */
#define MAIN_WIN_WIDTH	400
#define MAIN_WIN_HEIGHT	400

static int set_mode = -1; /* do not change mode */

static void get_screen_size(int *width, int *height)
{
	/* retrives screen size in pixels */
	/* ignores NULL parameters        */
	
	Display *disp = XOpenDisplay(NULL); /* get default display */


	if (NULL == disp) /* verify display is opened */
	{
		/* clear provided vars to avoid undefined results */
		if (NULL != width) *width = 0;
		if (NULL != height) *height = 0;
	}
	
	/* get screen dimentions */
	if (NULL != width) *width = DisplayWidth(disp, 0);
	if (NULL != height) *height = DisplayHeight(disp, 0);

	XCloseDisplay(disp); /* clean up */
}

static void exit_app(void *data, Evas_Object *obj, void *event_info)
{
	/* handles button clicks and window closure*/

	set_mode = (int)data; /* determine mode to set */
	elm_exit();
}

EAPI int elm_main(int argc, char **argv)
{
	Evas_Object 	*main_win, *bkg;   /* main window and backgroudn object*/
	Evas_Object 	*table;		   /* layout table for buttons */
	Evas_Object 	*btn_mode[4];	   /* 4 buttons for 4 to screen modes */
	Evas_Object	*icons[4];	   /* button icons */
	Evas_Object	*cancel_icon;	   /* X in red circle */
	Evas_Object	*cancel_button;    /* exit without orientation change */
	int		i;
	int		scr_width, scr_height;	/* screen dimentions */
	char		file_name[256];
	
	/* create main window and attach event hendler */
	main_win = elm_win_add(NULL, "scr_rotate", ELM_WIN_UTILITY);
	elm_win_shaped_set(main_win, FALSE);
	evas_object_smart_callback_add(main_win, "delete-request", exit_app,
								(void*)-1);
	evas_object_smart_callback_add(main_win, "focus-out", exit_app,
								(void*)-1);	

	/* add background to window */
	bkg = elm_bg_add(main_win);
	evas_object_size_hint_weight_set(bkg, 0.0, 0.0); /* inhibit expantion */
	evas_object_size_hint_min_set(bkg, MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT);
	evas_object_size_hint_max_set(bkg, MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT);	
	elm_win_resize_object_add(main_win, bkg); /* add to window */
	evas_object_show(bkg); /* make visible */
	
	/* create table for button placements */
	table = elm_table_add(main_win);
	evas_object_size_hint_weight_set(table, 1.0, 1.0);
	evas_object_size_hint_align_set(table, -1.0, -1.0);
	elm_win_resize_object_add(main_win, table);
	evas_object_show(table);

	/* create buttons */
	for (i = 0; i < 4; i++) /* create and show all buttons */
	{
		btn_mode[i] = elm_button_add(main_win);
		evas_object_size_hint_weight_set(btn_mode[i], 1.0, 1.0);
		evas_object_size_hint_align_set(btn_mode[i], -1.0, -1.0);

		/* setup button icont */
		snprintf(file_name, 256, /* format file name */
			"/usr/share/pixmaps/scr-rotate/mode_%d.png", i);

		/* match button with mode */
		icons[i] = elm_icon_add(btn_mode[i]);
		elm_icon_file_set(icons[i], file_name, NULL); /* load icon file */
		elm_icon_smooth_set(icons[i], TRUE);
		/* attach icon to button */
		elm_button_icon_set(btn_mode[i], icons[i]);
		
		evas_object_smart_callback_add(btn_mode[i], "clicked", exit_app,
									(void*)i);

		evas_object_show(btn_mode[i]);
	}

	/* insert buttons in to table at proper locations */
	elm_table_pack(table, btn_mode[0], 1, 0, 1, 1);
	elm_table_pack(table, btn_mode[1], 2, 1, 1, 1);
	elm_table_pack(table, btn_mode[3], 0, 1, 1, 1);
	elm_table_pack(table, btn_mode[2], 1, 2, 1, 1);
	
	/* create cancel button */
	cancel_button = elm_button_add(main_win);
	cancel_icon = elm_icon_add(cancel_button);
	elm_icon_file_set(cancel_icon,
				"/usr/share/pixmaps/scr-rotate/cancel.png", NULL);
	elm_icon_smooth_set(cancel_icon, TRUE);
	elm_button_icon_set(cancel_button, cancel_icon); /* add icon to button */
	evas_object_smart_callback_add(cancel_button, "clicked",
							exit_app, (void*)-1);
	evas_object_show(cancel_button);
	
	/* add cancel button to table */
	elm_table_pack(table, cancel_button, 1, 1, 1, 1);

	/* resize and reposition main window */
	elm_win_override_set(main_win, TRUE);
	
	/* center window */
	get_screen_size(&scr_width, &scr_height);
	evas_object_move(main_win, (scr_width - MAIN_WIN_WIDTH) / 2,
					(scr_height - MAIN_WIN_HEIGHT) / 2);
	
	evas_object_show(main_win); /* show main window */
	
	elm_run(); /* standard event loop */

	elm_shutdown(); /* cleanup */

	if (set_mode > -1)
	{
		snprintf(file_name, 256, "%d", set_mode);
		/* change to new screen mode if requested */
		execlp("xrandr", "xrandr", "-o", file_name, (char*)NULL);
	}

	return 0; /* always ok */
}

/* macro to replace main with Elementary initalization */
ELM_MAIN()
